package views.features;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.Border;

import views.util.GameColors;

@SuppressWarnings("serial")
public class LoginMenuView extends JPanel {

	protected JPanel main;
	protected GridBagConstraints gbc = new GridBagConstraints();
	protected JLabel usernameLabel = new JLabel("USERNAME");
	protected JLabel passwordLabel = new JLabel("PASSWORD");
	protected JLabel secretQuestionLabel = new JLabel("DOMANDA");
	protected JLabel secretAnswerLabel = new JLabel("RISPOSTA");
	protected JTextField userTextField;
	protected JPasswordField passTextField;
	protected JTextField secretQuestionTextField;
	protected JTextField secretAnswerTextField;
	protected JButton login = new JButton("ACCEDI AL MENU PRINCIPALE");
	protected JButton newAccount = new JButton("CREA UN NUOVO ACCOUNT");
	protected JButton recoveryPassword = new JButton("HAI DIMENTICATO LA TUA PASSWORD?");
	protected JButton build = new JButton("CREA L'ACCOUNT");
	protected JButton back = new JButton("TORNA INDIETRO");
	protected JButton recovery = new JButton("RECUPERA LA PASSWORD");
	protected final int textDim = 32;
	protected final Dimension buttonDimesion = new Dimension(250, 50);
	private JButton checkButton = new JButton("CONNESSIONE DATABASE RIUSCITA");
	private ActionListener currentActionListener;
	
	public LoginMenuView(String currentUser, String oldPassword) {
		login.setName("login");
		newAccount.setName("account");
		back.setName("indietro");
		build.setName("crea");
		recoveryPassword.setName("password");
		recovery.setName("recovery password");
		int fieldDim = 20;
		userTextField = new JTextField(fieldDim) {
			@Override
			public void setBorder(Border border) {
			}
		};
		passTextField = new JPasswordField(fieldDim) {
			@Override
			public void setBorder(Border border) {
			}
		};
		secretQuestionTextField = new JTextField(fieldDim) {
			@Override
			public void setBorder(Border border) {
			}
		};
		secretAnswerTextField = new JTextField(fieldDim) {
			@Override
			public void setBorder(Border border) {
			}
		};
		userTextField.setText(currentUser);
		passTextField.setText(oldPassword);
		this.setPreferredSize(new Dimension(800, 680));
		this.setLayout(new BorderLayout());
		main = new JPanel(new GridBagLayout());
		add(main, BorderLayout.CENTER);
		main.setBackground(Color.WHITE);
		buildLoginView();
		back.setFont(new Font("Segoe UI", Font.PLAIN, 35));
		userTextField.setBackground(GameColors.CENERE);
		passTextField.setBackground(GameColors.CENERE);
		secretQuestionTextField.setBackground(GameColors.CENERE);
		secretAnswerTextField.setBackground(GameColors.CENERE);
		login.setBackground(GameColors.ROSSO);
		newAccount.setBackground(GameColors.ROSSO);
		recoveryPassword.setBackground(GameColors.ROSSO);
		build.setBackground(GameColors.ROSSO);
		back.setBackground(GameColors.ROSSO);
		recovery.setBackground(GameColors.ROSSO);

		login.setForeground(Color.WHITE);
		newAccount.setForeground(Color.WHITE);
		recoveryPassword.setForeground(Color.WHITE);
		build.setForeground(Color.WHITE);
		back.setForeground(Color.WHITE);
		recovery.setForeground(Color.WHITE);
		usernameLabel.setForeground(GameColors.ROSSO);
		passwordLabel.setForeground(GameColors.ROSSO);

		back.setPreferredSize(buttonDimesion);
		back.setBorderPainted(false);
	}

	public JButton getNewAccount() {
		return newAccount;
	}

	public JButton getLogin() {
		return login;
	}

	public void signalProblem(boolean flag) {
		if (flag) {
			checkButton
					.setText("PROBLEMA CONNESSIONE AL DATABASE - CLICK PER RISOLVERLI");
			checkButton.setBackground(Color.RED);
			checkButton.addActionListener(currentActionListener);
		}
		if (!flag) {
			checkButton.setText("CONNESSIONE DATABASE RIUSCITA");
			checkButton.setBackground(Color.GREEN);
			checkButton.removeActionListener(currentActionListener);
			updateUI();
		}
	}

	public void buildLoginView() {
		main.removeAll();
		gbc = new GridBagConstraints();
		gbc.insets = new Insets(-30, 5, 50, 5); // Modificato da (15,5,5,5) per
												// compensare la presenza della
												// barra superiore

		gbc.gridx = 0;
		gbc.gridy = 0;
		usernameLabel.setFont(new Font("Segoe UI", Font.PLAIN, textDim));
		// usernameLabel.setForeground(Color.BLACK);
		main.add(usernameLabel, gbc);
		gbc.gridx = 1;
		gbc.gridy = 0;
		userTextField.setFont(new Font("Segoe UI", Font.PLAIN, textDim));
		main.add(userTextField, gbc);

		gbc.gridx = 0;
		gbc.gridy = 1;
		passwordLabel.setFont(new Font("Segoe UI", Font.PLAIN, textDim));
		// passwordLabel.setForeground(Color.BLACK);
		main.add(passwordLabel, gbc);
		gbc.gridx = 1;
		gbc.gridy = 1;
		passTextField.setFont(new Font("Segoe UI", Font.PLAIN, textDim));
		main.add(passTextField, gbc);

		gbc.gridx = 0;
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		login.setPreferredSize(buttonDimesion);
		login.setFont(new Font("Segoe UI", Font.PLAIN, 35));
		login.setBorderPainted(false);
		gbc.insets = new Insets(50, 5, 5, 5);
		main.add(login, gbc);

		gbc.gridx = 0;
		gbc.gridy = 3;
		gbc.insets = new Insets(10, 5, 5, 5);
		recoveryPassword.setFont(new Font("Segoe UI", Font.PLAIN, 25));
		recoveryPassword.setPreferredSize(buttonDimesion);
		recoveryPassword.setBorderPainted(false);
		main.add(recoveryPassword, gbc);

		gbc.gridx = 0;
		gbc.gridy = -1;
		gbc.insets = new Insets(25, 5, 5, 5);
		checkButton.setBackground(Color.GREEN);
		checkButton.setPreferredSize(new Dimension(100, 50));
		checkButton.setFont(new Font("Segoe UI", Font.BOLD, 21));
		main.add(checkButton, gbc);

		gbc.gridx = 0;
		gbc.gridy = 5;
		gbc.insets = new Insets(100, 5, 5, 5);
		newAccount.setPreferredSize(buttonDimesion);
		newAccount.setFont(new Font("Segoe UI", Font.PLAIN, 35));
		newAccount.setBorderPainted(false);
		main.add(newAccount, gbc);
	}

	public void addActionListenerToGame(ActionListener al) {
		currentActionListener = al;
		login.addActionListener(al);
		newAccount.addActionListener(al);
		recoveryPassword.addActionListener(al);
		build.addActionListener(al);
		back.addActionListener(al);
		recovery.addActionListener(al);

		passTextField.addActionListener(al);
		userTextField.addActionListener(al);
	}

	public String getUserTextField() {
		return userTextField.getText();
	}

	public String getsecretAnswerTextField() {
		return secretAnswerTextField.getText();
	}

	public String getsecretQuestionTextField() {
		return secretQuestionTextField.getText();
	}

	public String getPassTextField() {
		return new String(passTextField.getPassword());
	}

	public void showSecretAnswerError() {
		secretAnswerTextField.setForeground(Color.RED);
	}

	public void removeSecretAnswerError() {
		secretAnswerTextField.setForeground(Color.BLACK);
	}

	public void addKeyListenerToGame(KeyListener kl) {
		secretAnswerTextField.addKeyListener(kl);
		main.addKeyListener(kl);
		login.addKeyListener(kl);
		userTextField.addKeyListener(kl);
		passTextField.addKeyListener(kl);

	}

	public void setRecoveredPassword(String oldPassword) {
		passTextField.setText(oldPassword);

	}

	public void checkFocusMain() {
		main.requestFocus();
	}
	public void showMessage(String message) {
		JOptionPane.showMessageDialog(this, message);
	}
	
	public int showConfirm(){
		int i = JOptionPane.showConfirmDialog(this, "Hai già il file di Accesso al database?");
		return i;
	}

	public File getUserFile() {
		JFileChooser fc = new JFileChooser();
		fc.showDialog(this, "Seleziona");
		File f = fc.getSelectedFile();
		return f;
	}

	public String askUser() {
		return JOptionPane.showInputDialog("Inserisci lo Username che usi per connetterti al database");
	}

	public String askPassword() {
		return JOptionPane.showInputDialog("Inserisci la password che usi per connetterti al database");
	}

}
